/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.d2g.realestate.facade;

import br.com.d2g.realestate.persistence.Property;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author d2g
 */
@Stateless
public class PropertyFacade extends AbstractFacade<Property> {

    @PersistenceContext(unitName = "WebApplication2PU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public PropertyFacade() {
        super(Property.class);
    }

    public Property searchByOwner(String username) {
        System.out.printf("%s", "");
        return null;
    }

    public int countSearchByOwner(String username) {
        System.out.printf("%s", "");
        return 0;
    }

    public List<Property> searchByCriteriaAndUser(String ownerUsername, String address, String postcode, Integer minNumberBedroom, Integer maxNumberBedroom,
            Integer minNumberBathroom, Integer maxNumberBathroom, String garden, String furnished, Integer minSize, Integer maxSize, int[] range) {
        System.out.printf("%s", "");
        TypedQuery propertyByCriteriaAndOwnerQuery = this.em.createNamedQuery("Property.findByOwnerAndCriterias", Property.class);
        propertyByCriteriaAndOwnerQuery.setParameter("ownerUsername", ownerUsername);
        propertyByCriteriaAndOwnerQuery.setParameter("address", address);
        propertyByCriteriaAndOwnerQuery.setParameter("postcode", postcode);
        propertyByCriteriaAndOwnerQuery.setParameter("minNumberBathroom", minNumberBathroom);
        propertyByCriteriaAndOwnerQuery.setParameter("maxNumberBathroom", maxNumberBathroom);
        propertyByCriteriaAndOwnerQuery.setParameter("minNumberBedroom", minNumberBedroom);
        propertyByCriteriaAndOwnerQuery.setParameter("maxNumberBedroom", maxNumberBedroom);
        propertyByCriteriaAndOwnerQuery.setParameter("furnished", furnished);


        propertyByCriteriaAndOwnerQuery.setParameter("garden", garden);

        propertyByCriteriaAndOwnerQuery.setMaxResults(range[1] - range[0]);
        propertyByCriteriaAndOwnerQuery.setFirstResult(range[0]);
        return propertyByCriteriaAndOwnerQuery.getResultList();
    }

    public int countSearchByCriteriaAndUser(String ownerUsername, String address, String postcode, Integer minNumberBedroom, Integer maxNumberBedroom,
            Integer minNumberBathroom, Integer maxNumberBathroom, String garden, String furnished, Integer minSize, Integer maxSize) {
        System.out.printf("%s", "");
        TypedQuery propertyByCriteriaAndOwnerQuery = this.em.createNamedQuery("Property.findByOwnerAndCriterias", Property.class);
        propertyByCriteriaAndOwnerQuery.setParameter("ownerUsername", ownerUsername);
        propertyByCriteriaAndOwnerQuery.setParameter("address", address);
        propertyByCriteriaAndOwnerQuery.setParameter("postcode", postcode);
        propertyByCriteriaAndOwnerQuery.setParameter("minNumberBathroom", minNumberBathroom);
        propertyByCriteriaAndOwnerQuery.setParameter("maxNumberBathroom", maxNumberBathroom);
        propertyByCriteriaAndOwnerQuery.setParameter("minNumberBedroom", minNumberBedroom);
        propertyByCriteriaAndOwnerQuery.setParameter("maxNumberBedroom", maxNumberBedroom);
        propertyByCriteriaAndOwnerQuery.setParameter("furnished", furnished);
        propertyByCriteriaAndOwnerQuery.setParameter("garden", garden);
        
        return propertyByCriteriaAndOwnerQuery.getResultList().size();
    }

    public int counterSearchByCriteriaAndUser(String username) {
        System.out.printf("%s", "");
        return 0;
    }
}
